package com.supermap.dataservice.order;

import com.supermap.dataservice.order.dao.FunctionDao;
import com.supermap.dubbo.api.order.entity.Function;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.order.FunctionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:功能逻辑处理层
 * @Date:18/6/19 上午10:45
 */
@Service
public class FunctionServiceImpl implements FunctionService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(FunctionServiceImpl.class);

    @Resource
    private FunctionDao functionDao;


    @Override
    public JSONUtil getUserFunction(String userid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            List<Function> functions = functionDao.getAllFunctionByUserId(Integer.parseInt(userid));
            HashMap functionHM = new HashMap();
            if(functions!=null && functions.size()>0){
                List<Function> orderEd = new ArrayList<>();
                List<Function> noOrder = new ArrayList<>();
                for (Function f:functions) {
                    if("1".equals(f.getFlag())){
                        orderEd.add(f);
                    }else{
                        noOrder.add(f);
                    }
                }
                functionHM.put("orderEd",orderEd);
                functionHM.put("noOrder",noOrder);
            }
            //获得用户所有的功能信息后返回的json数据
            jsonUtil.setMsg("Get user functions success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(functionHM);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("FunctionServiceImpl=>getUserFunction=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("Get user functions flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil addUserFunction(String userid, String functionid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            functionDao.addUserFunction(Integer.parseInt(userid),Integer.parseInt(functionid));

            //增加用户的功能定制后返回的json数据
            jsonUtil.setMsg("add user functions success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData("");
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("FunctionServiceImpl=>addUserFunction=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("add user functions flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil checkUserFunctionExist(String userid, String functionid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            List<Function> functions = functionDao.checkUserFunctionExist(Integer.parseInt(userid),Integer.parseInt(functionid));

            if(functions!=null && functions.size()>0){
                jsonUtil.setData(true);
            }else{
                jsonUtil.setData(false);
            }
            //增加用户的功能定制后返回的json数据
            jsonUtil.setMsg("check user functions success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);

        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("FunctionServiceImpl=>checkUserFunctionExist=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("check user functions flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil delUserFunction(String userid, String functionid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {

            functionDao.delUserFunction(Integer.parseInt(userid),Integer.parseInt(functionid));

            //删除用户功能定制后返回的json数据
            jsonUtil.setMsg("del user functions success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData("");
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("FunctionServiceImpl=>delUserFunction=>DaoException:" + e.getMessage());
            jsonUtil.setMsg("del user functions flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
}
